<template>
    <div class="app-container">
          <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
            <el-form-item label="用户名称" prop="userName">
              <el-input
                v-model="queryParams.userName"
                placeholder="请输入用户名称"
                clearable
                style="width: 240px"
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
            <el-form-item label="手机号码" prop="phonenumber">
              <el-input
                v-model="queryParams.phonenumber"
                placeholder="请输入手机号码"
                clearable
                style="width: 240px"
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
            <el-form-item label="创建时间">
              <el-date-picker
                v-model="dateRange"
                style="width: 240px"
                value-format="yyyy-MM-dd"
                type="daterange"
                range-separator="-"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
              ></el-date-picker>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
              <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>
          <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
              <el-button
                type="danger"
                plain
                icon="el-icon-delete"
                size="mini"
                :disabled="multiple"
                @click="handleDelete"
                v-hasPermi="['system:user:remove']"
              >删除</el-button>
            </el-col>
          </el-row>
          <el-table v-loading="loading" :data="userList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="50" align="center" />
            <el-table-column label="用户名称" align="center" key="userName" prop="userName" :show-overflow-tooltip="true" />
            <el-table-column label="用户昵称" align="center" key="nickName" prop="nickName" :show-overflow-tooltip="true" />
            <el-table-column label="微信昵称" align="center" key="wcName" prop="wcName"  :show-overflow-tooltip="true" />
            <el-table-column label="微信号" align="center" key="wcNumber" prop="wcNumber" :show-overflow-tooltip="true" />
            <el-table-column label="手机号码" align="center" key="phonenumber" prop="phonenumber" width="120" />
            <el-table-column label="创建时间" align="center" prop="createTime" width="160">
              <template slot-scope="scope">
                <span>{{ parseTime(scope.row.createTime) }}</span>
              </template>
            </el-table-column>
            <el-table-column
              label="操作"
              align="center"
              width="160"
              class-name="small-padding fixed-width"
            >
              <template slot-scope="scope">
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-edit"
                  @click="handleUpdate(scope.row)"
                  v-hasPermi="['system:user:edit']"
                >审核</el-button>
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-delete"
                  @click="handleDelete(scope.row)"
                  v-hasPermi="['system:user:remove']"
                >删除</el-button>
              </template>
            </el-table-column>
          </el-table>
  
          <pagination
            v-show="total>0"
            :total="total"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="getList"
          />
  
      <!-- 添加或修改用户配置对话框 -->
      <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" key="getRandom" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="微信名称" prop="addUserFORM.wcName">
              <el-input v-model="form.addUserFORM.wcName" placeholder="请输入微信用户名称" maxlength="50" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="微信Id" prop="addUserFORM.wcId">
              <el-input v-model="form.addUserFORM.wcId" placeholder="请输入微信Id" maxlength="40" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="微信号" prop="addUserFORM.wcNumber">
              <el-input v-model="form.addUserFORM.wcNumber" placeholder="请输入微信号" maxlength="50" />
            </el-form-item>
          </el-col>
          <!-- <el-col :span="12">
            <el-form-item label="邮箱" prop="email">
              <el-input v-model="form.email" placeholder="请输入邮箱" maxlength="50" />
            </el-form-item>
          </el-col> -->
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item v-if="form.userId == undefined" label="用户名称" prop="addUserFORM.userName">
              <el-input v-model="form.addUserFORM.userName" placeholder="请输入用户名称" maxlength="30" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item v-if="form.userId == undefined" label="用户密码" prop="addUserFORM.password">
              <el-input v-model="form.addUserFORM.password" placeholder="请输入用户密码" type="password" maxlength="20" show-password/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="用户昵称" prop="addUserFORM.nickName">
              <el-input v-model="form.addUserFORM.nickName" placeholder="请输入用户昵称" maxlength="30" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="归属部门" prop="addUserFORM.deptId">
              <treeselect v-model="form.addUserFORM.deptId" :options="deptOptions" :show-count="true" placeholder="请选择归属部门" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="所属营业厅" prop="addUserFORM.busName">
              <el-select v-model="form.addUserFORM.busName" placeholder="请选择营业厅" @change="busChange">
                <el-option
                  v-for="dict in busInfo"
                  :key="dict.busId"
                  :label="dict.name"
                  :value="dict"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="手机号码" prop="addUserFORM.phonenumber">
              <el-input v-model="form.addUserFORM.phonenumber" placeholder="请输入手机号码" maxlength="11" />
            </el-form-item>
          </el-col>
        </el-row>
        <!-- <el-row>
          <el-col :span="24">
            <el-form-item label="微信头像" prop="wcImg">
              <el-input v-model="form.wcImg" placeholder="请输入微信头像url" maxlength="50" />
            </el-form-item>
          </el-col>
        </el-row> -->
       
        <el-row>
          <el-col :span="12">
            <el-form-item label="用户性别">
              <el-select v-model="form.addUserFORM.sex" placeholder="请选择性别">
                <el-option
                  v-for="dict in dict.type.sys_user_sex"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="状态">
              <el-radio-group v-model="form.addUserFORM.status">
                <el-radio
                  v-for="dict in dict.type.sys_normal_disable"
                  :key="dict.value"
                  :label="dict.value"
                >{{dict.label}}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="岗位">
              <el-select v-model="form.addUserFORM.postIds" multiple placeholder="请选择岗位">
                <el-option
                  v-for="item in postOptions"
                  :key="item.postId"
                  :label="item.postName"
                  :value="item.postId"
                  :disabled="item.status == 1"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="角色" prop="addUserFORM.roleIds">
              <el-select v-model="form.addUserFORM.roleIds" multiple placeholder="请选择角色">
                <el-option
                  v-for="item in roleOptions" 
                  :key="item.roleId"
                  :label="item.roleName"
                  :value="item.roleId"
                  :disabled="item.status == 1"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="审核" prop="status">
              <el-radio-group v-model="form.status">
                <el-radio
                  v-for="dict in dict.type.sys_examine"
                  :key="dict.value"
                  :label="dict.value"
                >{{dict.label}}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="备注">
              <el-input v-model="form.addUserFORM.remark" type="textarea" placeholder="请输入内容"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    </div>
  </template>
  
  <script>
  import { getApplyAccount,checkApplyAccount, deptTreeSelect,deleteApply } from "@/api/system/user";
  import { listRole} from "@/api/system/role";
  import { listPost} from "@/api/system/post";
  import { getToken } from "@/utils/auth";
  import Treeselect from "@riophae/vue-treeselect";
  import "@riophae/vue-treeselect/dist/vue-treeselect.css";
  export default {
    name: "User",
    dicts: ['sys_normal_disable', 'sys_user_sex','sys_examine'],
    components: { Treeselect },
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 用户表格数据
        userList: null,
        // 弹出层标题
        title: "",
        // 部门树选项
        deptOptions: undefined,
        // 是否显示弹出层
        open: false,
        // 部门名称
        deptName: undefined,
        // 默认密码
        initPassword: undefined,
        // 日期范围
        dateRange: [],
        // 岗位选项
        postOptions: [],
        // 角色选项
        roleOptions: [],
        // 表单参数
        form: {
          applyId:undefined, 
          status: undefined,
          addUserFORM: {
            status: undefined,
            userId: undefined,
            deptId: undefined,
            busId:undefined,
            busName:undefined,
            userName: undefined,
            nickName: undefined,
            password: undefined,
            phonenumber: undefined,
            wcId: undefined,
            wcName: undefined,
            wcNumber: undefined,
            wcImg: undefined,
            email: undefined,
            sex: undefined,
            status: "0",
            remark: undefined,
            postIds: [],
            roleIds: [],
          }
        },
        defaultProps: {
          children: "children",
          label: "label"
        },
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          status:undefined,
          userName: undefined,
          phonenumber: undefined,
          status: undefined,
          deptId: undefined
        },
        // 表单校验
        rules: {
          'addUserFORM.wcName': [
            { required: true, message: "微信用户名不能为空", trigger: "blur" },
          ],
          "addUserFORM.wcId": [
            { required: true, message: "微信Id不能为空", trigger: "blur" },
          ],
          "addUserFORM.wcNumber": [
            { required: true, message: "微信号不能为空", trigger: "blur" },
          ],
          "addUserFORM.userName": [
            { required: true, message: "用户名称不能为空", trigger: "blur" },
            { min: 2, max: 20, message: '用户名称长度必须介于 2 和 20 之间', trigger: 'blur' }
          ],
          "addUserFORM.password": [
            { required: true, message: "用户密码不能为空", trigger: "blur" },
            { min: 5, max: 20, message: '用户密码长度必须介于 5 和 20 之间', trigger: 'blur' }
          ],
          "addUserFORM.nickName": [
            { required: true, message: "用户昵称不能为空", trigger: "blur" }
          ],
          "addUserFORM.phonenumber": [
            {
              pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
              message: "请输入正确的手机号码",
              trigger: "blur"
            }
          ], 
          'addUserFORM.deptId': [
            { required: true, message: "部门不能为空", trigger: "blur" }
          ],
          'addUserFORM.roleIds': [
            { required: true, message: "角色不能为空", trigger: "blur" }
          ],
          status: [
            { required: true, message: "审核状态不能为空", trigger: "blur" }
          ],
        },
        busInfo:[],
      };
    },
    watch: {
      // 根据名称筛选部门树
      deptName(val) {
        this.$refs.tree.filter(val);
      }
    },
    created() {
      this.getList();
      this.getDeptTree();
      this.getListRole()
      this.getConfigKey("sys.user.initPassword").then(response => {
        this.initPassword = response.msg;
      });
    },
    methods: {
      /** 查询用户列表 */
      getList() {
        this.loading = true;
        getApplyAccount(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
            this.userList = response.data.records;
            this.total = response.data.total;
            this.loading = false;
          }
        );
      },
      getListRole(){
        listRole({ pageNum: 1,pageSize: 99,}).then(response => {
            response.rows.forEach(item => {
              if(item.roleId!=1){
                this.roleOptions.push(item)
              }
            });;
        });
        listPost({ pageNum: 1,pageSize: 99,}).then(response => {
            this.postOptions=response.rows;
        });
      },
      /** 查询部门下拉树结构 */
      getDeptTree() {
        deptTreeSelect().then(response => {
          // console.log(response)
          this.deptOptions = response.data;
        });
      },
      // 筛选节点
      filterNode(value, data) {
        if (!value) return true;
        return data.label.indexOf(value) !== -1;
      },
      // 节点单击事件
      handleNodeClick(data) {
        this.queryParams.deptId = data.id;
        this.handleQuery();
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const applyId = row.applyId || this.ids;
        this.$modal.confirm('是否确认删除用户编号为"' + applyId + '"的数据项？').then(function() {
          return deleteApply(applyId);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          applyId:undefined, 
          status: undefined,
          addUserFORM: {
            status: undefined,
            userId: undefined,
            deptId: undefined,
            busId:undefined,
            busName:undefined,
            userName: undefined,
            nickName: undefined,
            password: undefined,
            phonenumber: undefined,
            wcId: undefined,
            wcName: undefined,
            wcNumber: undefined,
            wcImg: undefined,
            email: undefined,
            sex: undefined,
            status: "0",
            remark: undefined,
            postIds: [],
            roleIds: [],
          }
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.dateRange = [];
        this.resetForm("queryForm");
        this.queryParams.deptId = undefined;
        this.$refs.tree.setCurrentKey(null);
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.applyId);
        this.single = selection.length != 1;
        this.multiple = !selection.length;
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        this.form = {
          applyId:row.applyId, 
          status: undefined,
          addUserFORM: {
            status: "0",
            userId: undefined,
            deptId: row.deptId,
            busId:undefined,
            busName:undefined,
            userName: row.userName,
            nickName: row.nickName,
            password: undefined,
            phonenumber: row.wcPhone,
            wcId: row.wcId,
            wcName: row.wcName,
            wcNumber: row.wcNumber,
            wcImg: undefined,
            email: undefined,
            sex: row.sex,
            remark: undefined,
            postIds: [],
            roleIds: []
          }
        };
        this.open = true;
        this.title = "审核用户";
        this.form.addUserFORM.password = this.initPassword;
        
      },
      /** 提交按钮 */
      submitForm: function(type) {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.applyId != undefined) {
              checkApplyAccount(this.form).then(response => {
                this.$modal.msgSuccess("操作成功");
                this.open = false;
                this.getList();
              });
            } 
          }
        });
      },
      busChange(e){
        //console.log(e)
          this.form.busId=e.busId
          this.form.busName=e.name
      },
      getBusInfo(){
        listBusInfo({pageNum:1,pageSize:999}).then(res => {
            //console.log(res)
            if(res.code==200){
              if(res.data){
                res.data.records.forEach(element => {
                  this.busInfo.push({busId:element.id,name:element.busName})
                });
              }
            }
          });
      },
    }
  };
  // 查询营业厅列表
  export function listBusInfo(query) {
    return request({
      url: '/wxassistant/wcbus_hall_info/page',
      method: 'get',
      params: query
    })
  }
  </script>
  